package com.webdemo9.dao.impl;

import java.io.InputStream;
import java.util.Properties;

import com.webdemo9.dao.UsersDao;

public class UsersDaoFactory {
	private static Properties p = null;
	
	/*
	 * 加载配置文件
	 */
	static {
		try {
			InputStream in = UsersDaoFactory.class.
					getClassLoader().getResourceAsStream("daoproperties.properties");
			p = new Properties();
			p.load(in);
		} catch(Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	public static UsersDao getUsersDao() {
		/*
		 * 给出一个配置文件，配置文件中给出UsersDao实现类的名称
		 * 通过本方法获取实现类的名称，通过反射完成对象的创建
		 */
		UsersDao udao = null;
		try {
			String className = p.getProperty("com.webdemo9.dao.UsersDao");
			Class<?> clazz = Class.forName(className);
			udao = (UsersDao)clazz.newInstance();
		} catch(Exception e) {
			throw new RuntimeException(e);
		}
		return udao;
	}
}
